package l1j.server.server.clientpackets;

import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.echo.ClientThread;

public abstract class ClientBasePacketOld {
	private static Logger _log = Logger.getLogger(ClientBasePacketOld.class.getName());

	private static final String CLIENT_LANGUAGE_CODE = Config.CLIENT_LANGUAGE_CODE;

	private byte _decrypt[];

	private int _off;

	public ClientBasePacketOld(byte abyte0[]) {
		_log.finest("type=" + this.getType() + ", len=" + abyte0.length);
		this._decrypt = abyte0;
		this._off = 1;
	}

	public ClientBasePacketOld(ByteBuffer bytebuffer, ClientThread clientthread) {
	}

	/**
	 * クライアントパケットの種類を表す文字列を返す。("[C] C_DropItem" 等)
	 */
	public String getType() {
		return "[C] " + this.getClass().getSimpleName();
	}

	public byte[] readByte() {
		final byte[] result = new byte[this._decrypt.length - this._off];
		try {
			System.arraycopy(this._decrypt, this._off, result, 0, this._decrypt.length - this._off);
			this._off = this._decrypt.length;
		} catch (final Exception e) {
			_log.log(Level.SEVERE, "OpCode=" + (this._decrypt[0] & 0xff), e);
		}
		return result;
	}

	public int readC() {
		final int i = this._decrypt[this._off++] & 0xff;
		return i;
	}

	public int readCH() {
		int i = this._decrypt[this._off++] & 0xff;
		i |= this._decrypt[this._off++] << 8 & 0xff00;
		i |= this._decrypt[this._off++] << 16 & 0xff0000;
		return i;
	}

	public int readD() {
		int i = this._decrypt[this._off++] & 0xff;
		i |= this._decrypt[this._off++] << 8 & 0xff00;
		i |= this._decrypt[this._off++] << 16 & 0xff0000;
		i |= this._decrypt[this._off++] << 24 & 0xff000000;
		return i;
	}


	public double readF() {
		long l = this._decrypt[this._off++] & 0xff;
		l |= this._decrypt[this._off++] << 8 & 0xff00;
		l |= this._decrypt[this._off++] << 16 & 0xff0000;
		l |= this._decrypt[this._off++] << 24 & 0xff000000;
		l |= (long) this._decrypt[this._off++] << 32 & 0xff00000000L;
		l |= (long) this._decrypt[this._off++] << 40 & 0xff0000000000L;
		l |= (long) this._decrypt[this._off++] << 48 & 0xff000000000000L;
		l |= (long) this._decrypt[this._off++] << 56 & 0xff00000000000000L;
		return Double.longBitsToDouble(l);
	}

	public int readH() {
		int i = this._decrypt[this._off++] & 0xff;
		i |= this._decrypt[this._off++] << 8 & 0xff00;
		return i;
	}

	/**
	 * 由byte[]中取回一個String
	 *
	 * @return
	 */
	protected String readS() {
		String s = null;
		try {
			if (this._decrypt == null) {
				return s;
			}
			s = new String(this._decrypt, this._off, this._decrypt.length - this._off,CLIENT_LANGUAGE_CODE);
			s = s.substring(0, s.indexOf('\0'));
			this._off += s.getBytes(CLIENT_LANGUAGE_CODE).length + 1;

		} catch (final Exception e) {
		}
		return s;
	}
}
